<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="primary"
                   size="small"
                   plain
                   v-if="permission.jmeter_upload"
                   icon="el-icon-upload2"
                   @click="handleUpload">上 传
        </el-button>
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.jmeter_delete"
                   @click="handleDelete">删 除
        </el-button>
      </template>
      <template slot-scope="scope" slot="menu">
        <el-button type="text"
                   icon="el-icon-video-play"
                   size="small"
                   v-if="permission.jmeter_run"
                   @click="handleRun(scope.row)">运行
        </el-button>
        <el-button type="text"
                   icon="el-icon-circle-check"
                   size="small"
                   v-if="permission.jmeter_download"
                   @click.stop="handleDownload(scope.row)">下载脚本
        </el-button>
        <el-button type="text"
                   icon="el-icon-document"
                   size="small"
                   v-if="permission.jmeter_download"
                   @click.stop="reportLoad(scope.row.id)">查看报告
        </el-button>
      </template>
    </avue-crud>
    <el-dialog title="上传脚本"
               append-to-body
               :visible.sync="attachBox"
               width="555px">
      <avue-form ref="form" :option="attachOption" v-model="attachForm" :upload-after="uploadAfter">
      </avue-form>
    </el-dialog>
    <el-dialog
      title="压测报告"
      :visible.sync="report"
      class="avue-dialog"
      v-dialogdrag
      width="60%">
      <avue-crud
        :option="reportOption"
        :data="reportData"
        @row-del="rowReportDel"
        @selection-change="selectionChange"
        ref="crud">
        <template slot="menuLeft">
          <el-button type="danger"
                     size="small"
                     icon="el-icon-delete"
                     plain
                     v-if="permission.jmeter_delete"
                     @click="handleReportDelete">删 除
          </el-button>
        </template>
        <template slot="reportName" slot-scope="{row}">
          <el-tag><a target="_blank" :href="host+row.saveUrl+row.reportName"> {{row.reportName}}</a></el-tag>
        </template>
        <template slot="jtlFileName" slot-scope="{row}">
          <el-tag><a target="_blank" :href="host+row.saveUrl+row.reportName+'/'+row.jtlFileName"> {{row.jtlFileName}}</a></el-tag>
        </template>
      </avue-crud>
      <span slot="footer" class="dialog-footer">
        <el-button @click="report=false">取 消</el-button>
        <el-button type="primary" @click="report=false">确 定</el-button>
      </span>
    </el-dialog>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove, run} from "@/api/desk/pts/jmeter";
  import {mapGetters} from "vuex";

  export default {
    data() {
      return {
        host: "http://123.125.252.121",
        form: {},
        query: {
          descs: 'update_time',
        },
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          lazy: true,
          height: "auto",
          calcHeight: 230,
          searchShow: true,
          searchMenuSpan: 6,
          tip: false,
          border: true,
          index: true,
          addBtn: false,
          addRowBtn: false,
          cellBtn: true,
          selection: true,
          menuWidth: 400,
          dialogDrag: true,
          column: [
            {
              label: "压测脚本名称",
              prop: "jmxName",
              rules: [{
                required: true,
                message: "请输入脚本名称",
                trigger: "blur"
              }]
            },
            {
              label: "压测脚本描述",
              prop: "description",
              cell: true,
              // cancelBtn: true,
              // rules: [{
              //   required: true,
              //   message: "压测脚本描述",
              //   trigger: "blur"
              // }]
            },
            {
              label: "上次执行时间",
              prop: "updateTime",
              rules: [{
                required: true,
                message: "请输入在线报告",
                trigger: "blur"
              }]
            },
            {
              label: "创建时间",
              prop: "createTime",
            },
          ]
        },
        data: [],
        report: false,
        reportForm: {},
        reportData: [],
        reportOption: {
          lazy: true,
          // height: "auto",
          searchShow: true,
          searchMenuSpan: 6,
          tip: false,
          border: true,
          index: true,
          addBtn: false,
          addRowBtn: false,
          editBtn: false,
          selection: true,
          dialogDrag: true,
          column: [
            {
              label: "JTL报告",
              prop: "jtlFileName",
            },
            {
              label: "在线报告",
              prop: "reportName",
              type: 'url',
            },
            {
              label: "执行时间",
              prop: "createTime",
            },
          ]
        },
        attachBox: false,
        attachForm: {},
        attachOption: {
          submitBtn: false,
          emptyBtn: false,
          column: [
            {
              // label: '附件上传',
              prop: 'attachFile',
              type: 'upload',
              drag: true,
              loadText: '上传中，请稍等',
              span: 24,
              propsHttp: {
                res: 'data'
              },
              action: "/api/pts/jmeter/upload_jmx"
            }
          ]
        }
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.jmeter_add, false),
          viewBtn: this.vaildData(this.permission.jmeter_view, false),
          delBtn: this.vaildData(this.permission.jmeter_delete, false),
          editBtn: this.vaildData(this.permission.jmeter_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      },
    },
    methods: {
      handleDownload(row) {
        // window.open(location.protocol + '//' + location.hostname + `${row.jmxUrl}`);
        window.open(this.host+`${row.saveUrl}` + `${row.jmxFileName}`);
      },
      handleRun(row, loading) {
        this.loading = true;
        run(row).then(res => {
          this.loading = false;
          this.$message({
            type: "success",
            message: res.data.msg
          });
        }, error => {
          this.loading = false;
          window.console.log(error);
          loading();
        });
      },
      rowReportDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.report = false;
            this.reportLoad(row.parentId);
            this.report = true;
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleReportDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.report=false
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      reportLoad(parentId) {
        this.reportData=[];
        const reportParams = this.query;
        reportParams.parentId = parentId;
        getList(1, 10, Object.assign(reportParams, this.query)).then(res => {
          const reportData = res.data.data;
          this.reportData = reportData.records;
          this.report = true;
          this.query = {descs: 'update_time',};
        });
      },
      handleUpload() {
        this.attachBox = true;
      },
      uploadAfter(res, done, loading, column) {
        window.console.log(column);
        this.attachBox = false;
        this.refreshChange();
        done();
      },
      rowSave(row, done, loading) {
        add(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },

      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage) {
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize) {
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        params.parentId = 0;
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

